from libavg import avg
import math
from threading import Timer
from config import Config

class Wheel(avg.ImageNode):
    
    __localMidPoint = 0, 0 
    __speed = 0
    __pointsbar = None # VERWALTET PUNKTE
    __initialpos = None
    rotor = None 
    anim = None # stores current animation
    
    #set dynamic
    radius = 0
    def __init__(self, posi, par):   
        
        self.rotor = avg.ImageNode.__init__(self, href="../data/img/rotor_720.png",
                                             pos = (posi[0] - 233 / 2,
                                                    posi[1] - 233 / 2) , parent = par, pivot= (116.5, 116.5))        
        self.__pointsbar = None
        self.__t = None
        self.__bool = False
        self.__eventCapture = None
        self.setEventHandler(avg.CURSORDOWN, avg.TOUCH, self._enter)
        self.setEventHandler(avg.CURSORMOTION, avg.TOUCH, self._capture)
        self.setEventHandler(avg.CURSORUP, avg.TOUCH, self._leave)    
        
        #midX = posi[0] + 233 / 2    
        #midY = posi[1] + 233 / 2 
        self.__localMidPoint = posi
        
        self.radius = Config.resX/14
    
	#public function for later assigning of the pointsbar:
    def setPointsbar(self, pointsb):
        self.__pointsbar = pointsb
    
    def increasePoints(self):
        if self.__pointsbar is not None:
            self.__pointsbar.addPoints(1)
        
        
    #function for calculating distance between two 2DPoint Objects, requires import math:
    def getDistance(self, p, q): 
        return math.sqrt((p.x-q.x)**2 + (p.y-q.y)**2)     
    
    def getWheelDistance(self, x, y): 
        return math.sqrt((self.__localMidPoint[0] - x)**2 + (self.__localMidPoint[1] - y)**2)      
    
  ########## TOUCH EVENT ###########
    def _enter(self, event):
        self.__eventCapture = event
        
    def _capture(self, event):
        if self.__eventCapture is not None:
            initialpos = self.__eventCapture.pos #save initial cursor position        
            if event.cursorid == self.__eventCapture.cursorid and initialpos is not None:
                #filter dragging:
                if initialpos.x < event.pos.x and initialpos.y < self.pos.y+116\
                    or initialpos.x > event.pos.x and initialpos.y > self.pos.y+116\
                    or initialpos.y < event.pos.y and initialpos.x > self.pos.x+116\
                    or initialpos.y > event.pos.y and initialpos.x < self.pos.x+116:
                    self.__bool = True
    
    def _leave(self, event):
        if self.__bool:
            self.rotate()
        self.__eventCapture = None
        self.__bool = False
  ######### END TOUCH EVENT #########
    
    # decrement wheel speed:
    def slowdown(self):
        avg.Player.get().clearInterval(self.__t)
        self.__speed -= 1 if self.__speed>0 else 0
        self.anim=None
        #print "slowing down, new speed: "+str(self.__speed)
    
	# wheel rotation management:
    def rotate(self):
        if self.anim is not None:
            sp = self.__speed # save previous speed
            self.anim.abort() # abort previous animation
            self.__speed = sp + (1 if sp<10 else 0) # restore previous speed and increment it
            #print "continued"
        else:
            #print "started"
            self.__speed = 1           
        self.__t = avg.Player.get().setInterval(100,self.increasePoints)
        dur = 2200+self.__speed*50
        eo = 1100+dur//3
        self.anim = avg.EaseInOutAnim(self, #node 
                                      'angle', #attrName
                                      dur, #duration
                                      self.angle, # startValue
                                      self.angle + 3.14*self.__speed, #endValue
                                      0,#easeInDuration
                                      eo, #easeOutDuration
                                      #startCallback=None
                                      #stopCallback=None
                                      )
        self.anim.setStopCallback(self.slowdown)
        self.anim.start()
        
        